package com.smart.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.smart.domain.Role;
import com.smart.dto.RegisterDto;
import com.smart.service.RoleService;
import com.smart.service.UserService;
import com.smart.tools.ResponseMsg;

/**
 * 
 * 版权所有：xxxxxxx
 * @Description: 用户
 * @Title: UserController.java 
 * @ClassName: UserController 
 * @author tangyan 
 * @date 2019年2月13日 下午7:29:26 
 * @version V1.0
 */
@Controller
@RequestMapping("/user")
public class UserController {

	@Autowired
	UserService userService;
	
	@Autowired
	RoleService roleService;
	
	@RequestMapping(value = "/toRegister")
    public ModelAndView toRegister(HttpServletRequest request) throws Exception {
		//查询系统角色
		List<Role> roles  = roleService.queryRole();
		ModelMap map = new ModelMap();
		map.put("roles", roles);
		return new ModelAndView( "register",map);
	}
	
	/**
	 * 
	 * @Title: register 
	 * @Description: 注册
	 * @author tangyan 
	 * @param request
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "/register")
    @ResponseBody
    public ResponseMsg register(RegisterDto registerDto)  {
		ResponseMsg msg = new ResponseMsg();
		
		try {
			userService.register(registerDto);
			msg.setMsg("注册成功");
			msg.setStatus(ResponseMsg.RESULT_STATUS_SUCESS);
		} catch (Exception e) {
			msg.setMsg("注册失败"+e.getMessage());
			msg.setStatus(ResponseMsg.RESULT_STATUS_ERROR);
		}
		return msg;
	}
}
